from connect.conn_mysql import Connect


class SaveCodes(Connect):

    def save(self,phone,pass_word):
        '''保存用户的账号和验证码到数据库'''
        save_sql = '''
        insert into user(phone,password)
        values(%s,%s)
        '''
        self.insert(save_sql,(phone,pass_word))

    def send_masg(self):
        '''发送短信的函数'''
        return {
            'status':1,
            'errorCode':None,
            'errorMsg':None,
            'resultBody':'发送信息成功'
        } 

    def save_codes(self,phone,phone_verify_code):
        '''保存电话和验证码到verify_code表中'''
        save_code_sql='''
        insert into verify_code(phone,phone_verify_code)values(%s,%s)
        '''
        self.insert(save_code_sql,(phone,phone_verify_code))

    def send_msg(self):
        '''发送注册成功的信息'''
        return {
            'status':1,
            'errorCode':None,
            'errorMsg':None,
            'resultBody':'注册成功'
        }

    def register(self,phone,phone_verify_code,pass_word):
        '''用户注册的详细逻辑函数'''
        register_sql = '''
        select * from user where phone = %s
        '''
        verify_sql = '''
        select * from verify_code where phone_verify_code = %s
        '''
        register_sql_result = self.select_one(register_sql,(phone,))
        verify_sql_result = self.select_one(verify_sql,(phone_verify_code,))
        if register_sql_result:return{
                    'status':-1,
                    'errorCode':402,
                    'errorMsg':'你的号码已经被注册,请联系移动公司',
                    'resultBody':'注册失败'
        }
        if verify_sql_result==None and register_sql_result==None:return{
                    'status':-1,
                    'errorCode':403,
                    'errorMsg':'你输入的验证码有误,请重新输入',
                    'resultBody':'注册失败'
        }

        if register_sql_result==None and verify_sql_result:
            self.save(phone,pass_word)
            return{
                    'status':1,
                    'errorCode':None,
                    'errorMsg':None,
                    'resultBody':'注册成功'
            }
        
       
